﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace Clients
{
    public class SerializerHelper
    {
        /// <summary>
        /// Json序列化对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string JsonSerialize(object obj)
        {
            if (obj == null) { return null; }
            JavaScriptSerializer jss = new JavaScriptSerializer();
            jss.MaxJsonLength = 1024000000;
            //jss.RegisterConverters(new JavaScriptConverter[] { new WEJavaScriptConverter() });
            string json = jss.Serialize(obj);
            return json;
        }
         
        /// <summary>
        /// Json反序列化对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T JsonDeserialize<T>(string json)
        {
            if (string.IsNullOrEmpty(json)) { return default(T); }
            JavaScriptSerializer jss = new JavaScriptSerializer();
            T obj = jss.Deserialize<T>(json);
            return obj;
        }
    }
}
